#include "data_deal.h"
#include "logic.h"
#include "stdlib.h"
#include "modbus.h"
#include <string>
#include "rtc.h"
using namespace std;
bool dealFlag = false;
extern "C" void Uart1_Data_Deal()
{
    u1Buff[u1Len] = '\0';
    string ss = (char*)u1Buff;
    u1Len = 0;
    int firstPosition = ss.find("GETVT:");
    int secondPosition = ss.find("#");
    if(firstPosition != -1 && secondPosition != -1)
    {
        dealFlag = true;
        ss = ss.substr(firstPosition+6,secondPosition-firstPosition-6);
        if(ss.length() > 5) printf("wrong addr\r\n");
        else
        {
            uint8_t tmpAddr = atoi(ss.c_str());
            if(tmpAddr == gSrcAddr || tmpAddr == 0)
            {
                gSendFlag = 1;
                gState = INTERRUPT_FROM_LORA;
            }
        }
    }      
    firstPosition = ss.find("SETMODE:");
    secondPosition = ss.find("#");
    if(firstPosition != -1 && secondPosition != -1)
    {
        dealFlag = true;
        ss = ss.substr(firstPosition+8,secondPosition-firstPosition-8);
        if(ss.length() > 5) printf("wrong addr\r\n");
        else
        {
            firstPosition = ss.find(",");
            if(firstPosition != -1)
            {
                string ss1 = ss.substr(0,firstPosition);
                uint8_t tmpAddr = atoi(ss1.c_str());
                ss = ss.substr(firstPosition+1);
                uint8_t mode = atoi(ss.c_str());
                if(tmpAddr == gSrcAddr || tmpAddr == 0)
                {
                    printf("ACKMODE:%d,%d#\r\n",gSrcAddr,mode);
                    if(mode == 1 && gMode == 0)
                    {
                        Set_Rtc_Time();
                        RTC_Switch_RTCA();
                    }
                    gMode = mode ;
                }
            }
        }
    }
    firstPosition = ss.find("GETMODE:");
    secondPosition = ss.find("#");
    if(firstPosition != -1 && secondPosition != -1)
    {
        dealFlag = true;
        ss = ss.substr(firstPosition+8,secondPosition-firstPosition-8);
        if(ss.length() > 5) printf("wrong addr\r\n");
        else
        {
            uint8_t tmpAddr = atoi(ss.c_str());
            if(tmpAddr == gSrcAddr || tmpAddr == 0)
            {
                printf("ACKMODE:%d,%d#\r\n",gSrcAddr,gMode);
            }
        }
    }
    dealFlag = false;
}
